﻿using System;
using System.Collections.Generic;
using System.Text;
using LabMS;

namespace Base
{
    public class PrivilegeManager
    {
        /// <summary>
        /// GetCurrentUserInfo
        /// </summary>
        /// <returns></returns>
        public static bool GetCurrentUserInfo()
        {
            if (System.Web.HttpContext.Current.Session != null)
            {
                if (System.Web.HttpContext.Current.Session["User"] != null &&
                    !string.IsNullOrEmpty(System.Web.HttpContext.Current.Session["User"].ToString()) &&
                    System.Web.HttpContext.Current.Session["UserType"] != null &&
                    !string.IsNullOrEmpty(System.Web.HttpContext.Current.Session["UserType"].ToString()))
                {
                    if (System.Web.HttpContext.Current.Session["UserType"].ToString().Equals("mana"))
                    {
                        object obj = System.Web.HttpContext.Current.Session["User"];
                        LabMS.Model.UserTable u = obj as LabMS.Model.UserTable;
                        System.Web.HttpContext.Current.Session["UserID"] = u.UserId;
                        System.Web.HttpContext.Current.Session["UserName"] = u.UserName;
                        System.Web.HttpContext.Current.Session["UserCode"] = u.PCode;
                        if (System.Web.HttpContext.Current.Session["UserPrivilege"] == null)
                        {
                            if (u.UserName == "admin")
                            {
                                LabMS.BLL.Function bf = new LabMS.BLL.Function();
                                System.Web.HttpContext.Current.Session["UserPrivilege"] = bf.GetModelList("");
                            }
                            else
                            {
                                System.Web.HttpContext.Current.Session["UserPrivilege"] = ExtendBLL.Privilege.GetManaFunctionByUserID(u.UserId.ToString());
                            }

                        }
                    }
                    else if (System.Web.HttpContext.Current.Session["UserType"].ToString().Equals("student"))
                    {
                        object obj = System.Web.HttpContext.Current.Session["User"];
                        LabMS.Model.Student s = obj as LabMS.Model.Student;
                        System.Web.HttpContext.Current.Session["UserID"] = s.ID;
                        System.Web.HttpContext.Current.Session["UserName"] = s.Student_Name;
                        System.Web.HttpContext.Current.Session["UserCode"] = s.Student_Code;
                    }
                    else if (System.Web.HttpContext.Current.Session["UserType"].ToString().Equals("teacher"))
                    {
                        object obj = System.Web.HttpContext.Current.Session["User"];
                        LabMS.Model.Teacher t = obj as LabMS.Model.Teacher;
                        System.Web.HttpContext.Current.Session["UserID"] = t.ID;
                        System.Web.HttpContext.Current.Session["UserName"] = t.Teacher_Name;
                        System.Web.HttpContext.Current.Session["UserCode"] = t.Teacher_Code;
                    }

                    return true;
                }
            }
            return false;
        }

        public static int GetUserTypeCode()
        {
            int code = int.MinValue;
            if (System.Web.HttpContext.Current.Session != null && System.Web.HttpContext.Current.Session["UserType"] != null)
            {
                switch (System.Web.HttpContext.Current.Session["UserType"].ToString())
                {
                    case "mana":
                        code = 0;
                        break;
                    case "teacher":
                        code = 1;
                        break;
                    case "student":
                        code = 2;
                        break;
                    default:
                        break;
                }
            }

            return code;
        }


    }
}

